//接口地址匹配测试环境
function _urlMatching(url){

    var urlString = window.location.href;
    //接口匹配环境
    if(url.toString().indexOf('http://') == -1 && url.toString().indexOf('https://') == -1){

        if(urlString.indexOf('https://') != -1){
            if(urlString.indexOf('52tt.com/project_test/') != -1){
                return 'https://gray.52tt.com' + url;
            }
            else if(urlString.indexOf('52tt.com') != -1){
                return 'https://api.52tt.com' + url;
            }
            else {
                return 'http://192.168.9.230:8089' + url;
            }
        }else {
            if(urlString.indexOf('52tt.com/project_test/') != -1){
                return 'http://gray.52tt.com' + url;
            }
            else if(urlString.indexOf('52tt.com') != -1){
                return 'http://api.52tt.com' + url;
            }
            else {
                return 'http://192.168.9.230:8089' + url;
            }
        }

    }else {
        return url
    }
}

//请求TT的JSON数据
//参数: url: 例如'/act/goddess/post',地址如http://api.52tt.com会自动补齐
//其他参数:
// parameter: Object 请求参数, success: Function, Indicator: 菊花类型 (true表示不显示菊花, 'bottom'表示使用底部菊花), failure: Function
myApp.requestApi = function (url, parameter, success, Indicator, failure){
    if(parameter == null){
        parameter = {};
    }

    parameter.callback = 1;
    parameter.debug = 0;

    var inApp = myWebview.isInAppFunc();
    if(inApp){
        parameter.token = myWebview.getAppData("getToken");
    }

    //透传参数
    var urlString = window.location.href;
    var extendParameter = $$.parseUrlQuery(urlString);
    //过滤参数里的hash #
    extendParameter = _.mapObject(extendParameter, function(val, key) {
        if(val.toString().indexOf('#') != -1){
            return val.toString().substr(0, val.toString().indexOf('#'));
        }else {
            return val;
        }
    });
    parameter = _.extend(parameter, extendParameter);

    //接口匹配环境
    url = _urlMatching(url);

    //菊花类型
    myApp.showLoadingIndicator(Indicator);


    $$.ajax({
        method: "get",
        async: true,
        url: url,
        crossDomain: true,
        dataType: 'json',
        timeout: 30 * 1000,
        data: parameter,
        success: function (data) {
            myApp.hideLoadingIndicator(Indicator);

            if (data.code === 0) {
                if(success) success(data);
            }else{
                //菊花是否静默
                if(Indicator){
                    //myApp.alert(data.msg + " (" + data.code + ")" );
                }else {
                    myWebview.showToast(data.msg + " (" + data.code + ")");
                }

                if(failure) failure(data)

            }
        },
        error: function () {

            myApp.hideLoadingIndicator(Indicator);

            if(Indicator){

            }else{
                myApp.networkErrorProcess();
            }
            console.log('error-page');

        }
    });
}


//POST JSON数据
//参数: url: 例如'/act/goddess/post',地址如http://api.52tt.com会自动补齐
// parameter: Object 请求参数, success: Function, Indicator: 菊花类型 (true表示不显示菊花, 'bottom'表示使用底部菊花), failure: Function
myApp.requestPostApi = function (url, parameter, success, failure, Indicator){

    if(parameter == null){
        parameter = {};
    }

    var inApp = myWebview.isInAppFunc();
    if(inApp){
        parameter.token = myWebview.getAppData("getToken");
    }

    //透传参数
    var urlString = window.location.href;
    var extendParameter = $$.parseUrlQuery(urlString);
    //过滤参数里的hash #
    extendParameter = _.mapObject(extendParameter, function(val, key) {
        if(val.toString().indexOf('#') != -1){
            return val.toString().substr(0, val.toString().indexOf('#'));
        }else {
            return val;
        }
    });
    parameter = _.extend(parameter, extendParameter);

    //接口匹配环境
    url = _urlMatching(url);

    //菊花类型
    myApp.showLoadingIndicator(Indicator);

    $$.ajax({
        method: "post",
        async: true,
        url: url,
        crossDomain: true,
        dataType: 'json',
        timeout: 30 * 1000,
        data: JSON.stringify(parameter),
        success: function (data) {
            myApp.hideLoadingIndicator(Indicator);

            if (data.code === 0) {
                if(success) success(data);
            }else{
                //菊花是否静默
                if(Indicator){
                    //myApp.alert(data.msg + " (" + data.code + ")" );
                }else {
                    myWebview.showToast(data.msg + " (" + data.code + ")");
                }

                if(failure) failure(data)

            }
        },
        error: function () {
            myApp.hideLoadingIndicator(Indicator);

            if(Indicator){

            }else{
                myApp.networkErrorProcess();
            }
            console.log('error-page');

        }
    });
}


//跳转错误页
myApp.networkErrorProcess = function(){
    console.log('网络异常，请求失败');
    //myApp.redictNewPage('network-error', true, false);
    myWebview.showToast('网络异常，请检查你的网络连接后重试');
}

//菊花类型显示
myApp.showLoadingIndicator = function(type) {
    if(type != true){
        if(type == 'bottom'){
            myApp.showBottomIndicator();
        }
        else{
            myApp.showIndicator();
        }
    }
}

//菊花关闭
myApp.hideLoadingIndicator = function(type) {
    if(type != true){
        if(type == 'bottom'){
            myApp.hideBottomIndicator();
        }
        else{
            setTimeout(function(){
                myApp.hideIndicator();
            }, 300);
        }
    }

    setTimeout(function(){
        myApp.pullToRefreshDone();
    }, 500);
}
